//
//  CallTraceMonitor.m
//  DemoV2
//
//  Created by 董一龙 on 2022/8/5.
//

#import "CallTraceMonitor.h"
#include "ObjcHookCore/HookCore/HookCore.hpp"

@implementation CallTraceMonitor

+ (NSString *)getLogFileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
   
    NSDate *date = [NSDate date];
    NSDateFormatter *dataFormater = [[NSDateFormatter alloc] init];
    dataFormater.dateFormat = @"yyyy-MM-dd";
    NSString *fileName = [NSString stringWithFormat:@"%@.log",[dataFormater stringFromDate:date]];
    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
    NSLog(@"%s log in %@", __FUNCTION__, logFilePath);
    return logFilePath;
}

+ (void)startMonitor{
    NSLog(@"Hook objc_msgSend %s\n", __FUNCTION__);
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        HookCore::config().log_filename = [[CallTraceMonitor getLogFileName] UTF8String];
        NSLog(@"日志文件: %s", HookCore::config().log_filename.c_str());
        HookCore::startHook();
    });
}

+ (void)stopMonitor{
    NSLog(@"Hook objc_msgSend %s\n", __FUNCTION__);
    HookCore::stopHook();
}

+ (void)disableLog{
    NSLog(@"Hook objc_msgSend %s\n", __FUNCTION__);
    HookCore::disableLog();
}

+ (void)enableLog{
    NSLog(@"Hook objc_msgSend %s\n", __FUNCTION__);
    HookCore::enableLog();
}

+ (HookCore::Config) config{
    return HookCore::Config();
}

+ (HookCore::Config) config:(HookCore::Config) config{
    return HookCore::Config() = config;
}
@end
